<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script th:src="@{/password_security/crypto-js-develop/src/rsa.js}"></script>
    <script th:src="@{/password_security/crypto-js-develop/src/crypto-js.js}"></script>
    <script th:src="@{/password_security/crypto-js-develop/src/core.js}"></script>
    <script th:src="@{/password_security/crypto-js-develop/src/evpkdf.js}"></script>
    <script th:src="@{/password_security/crypto-js-develop/src/cipher-core.js}"></script>
    <script th:src="@{/password_security/crypto-js-develop/src/enc-base64.js}"></script>
    <script th:src="@{/password_security/crypto-js-develop/src/enc-base64url.js}"></script>
    <script th:src="@{/password_security/crypto-js-develop/src/enc-utf16.js}"></script>
    <!--<script src="https://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/aes.js"></script>-->
</head>
<body>

</body>
<script>
    var encrypt = new JSEncrypt();
    //获取私钥
    var private = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMWsnge1ll+ze0CuWLcRw58OY2GAUKAXC1GA15EvroIY4O+T6C3DL7ZtWs2NbOhFn4Ps6upDANvIjhMQfhKb9qdVGBS4elXTPAaV8mM5FHeUAsum6YCuwSSVA9boyvqeN8oeTCrhw4iLcabfoWpeZzV4wXlkWkhSMn6aTs7MPrevAgMBAAECgYBuOx486lQatfmrTg4GuI8/6UQeIPHU9ETJhHGxcsd7oOf+cZptuoSm1EBok88Bj6GubbVqQUb/SM2BcBb9xZ87H+MkGXtDZIlCgosWsLvjkVFMUzjg8hlhI2bnhmWGyMguK0CRAHXGzXpYN1VaD/b2ig2PphsRmi21vTgGzv8VwQJBAP8xXiCheAu3wdm/1VOLgac4n9DmSVA2QLqziOTR3WU/I8o6h2Stq27W3UtTRZRb7Gx6weI+DIDTWlOpa2Mm9pECQQDGTK0eQtRzRYL0K1ENtfxf//FwktfNULaDBMkm64fK4ECJn3xIPDaqzzpfvmTcTjv/bbjXbLGTM4QVAtxykGo/AkEA55aSuIQwoFmz53uLClV5ZRsjFf5XHB8QIR8bJaU7CU4Cmd0B0CE3idAeZeR9KYU+irfgygPLbwHfYDtJBGNwsQJBAKlmg1WgncFDlpVkE7/0wX55NRaIO1aoJuJSSky3VK/ZVwi6d/91gPX2RdXY3mzdYJ/aadeOgTxQ+/q/EPfFqx0CQHK3+ql2gEy/sCaXxkK6TnpP7MATmEYQl1sO4xbjhZ2lQ8mul1vTBEQVIohgMy2uoOoxuywGMZZBoHYq1Y4r6IE=\n"
    //var public = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrDysnndm5BjfWlc4jaBqkRyf7rtpBAga2u14Fv1XGlLqa7swTuqVPinUBMGPXufII0NNv8C/b99ZYsrhZMNIkYhUuWeCsPDvsSjcdUhwuwGSnjWw2l2Q4z40o7s8AgeUSlEH1xRNrcZKzVQiT+jhxtl8KMpZWiLAZojLzp0NDdQIDAQAB\n"
    //encrypt.setPublicKey(private)
    encrypt.setPrivateKey(private)

    //解码base64加密字符串
    var randomStringEncodeByDecodeRsaPublicKeyWithBase64 = "RWRFbWZJQURublNZT2dlMzVwQ0RJTldqNUIrRTN3V3ZGYmxsR1BiQmQyaDNLS2xoTFhnK1FPRG1zNm9VSWZsSzZ5cUVHeFhndnN1dVI4ZTkzU3Njbko3ZVAweGF4VXExZFIrMnM0K1BicXo0SmxEOUNKWVdoeGVzVWpMQ2lvbnVrWEFqbTZMalMxZVl1ZWQvRDFpYlJMdFVPWXlxVzZEUE1Ed0xBL0NsSHJvPQ==\n";
    var randomStringDecodeByDecodeRsaPublicKeyWithBase64 = window.atob(randomStringEncodeByDecodeRsaPublicKeyWithBase64).toString();

    console.log("解码base64加密字符串：" + randomStringDecodeByDecodeRsaPublicKeyWithBase64)

    //私钥解密字符串
    var originRandomString = encrypt.decrypt(randomStringDecodeByDecodeRsaPublicKeyWithBase64);
    console.log("原生字符串" + originRandomString)

    //密码加字符串，AES对称加密，以原生字符串作密钥

    //1.偏移值保持与后端相同
    var iv  = CryptoJS.enc.Utf8.parse("0123456789ABCDEF");

    //2.原生字符串+密码 为内容，原生字符串作密钥，AES加密
    var password = "123";
    var aesEncrypt = CryptoJS.AES.encrypt(originRandomString+password,   CryptoJS.enc.Utf8.parse(originRandomString),{
        iv:iv,
        mode:CryptoJS.mode.CBC,
        padding:CryptoJS.pad.Pkcs7
    });

    //直接给这个给后端，返回的是base64
    console.log("AES加密后aesEncrypt = %s"+aesEncrypt.toString())
    /*console.log(aesEncrypt.iv.toString(CryptoJS.enc.Base64))*/

    var aesDecrypt = CryptoJS.AES.decrypt(aesEncrypt, CryptoJS.enc.Utf8.parse(originRandomString) , {
        iv:iv,
        mode:CryptoJS.mode.CBC,
        padding:CryptoJS.pad.Pkcs7
    });
    console.log("AES解密后aesDecrypt = %s", aesDecrypt.toString(CryptoJS.enc.Utf8));

    //base64编码
    let decodeByBase64 = window.btoa(aesEncrypt);
    console.log("base64编码AES加密字符串：" + decodeByBase64)
    /* console.log(hex_to_bin(s))*/

    /* function hex_to_bin(str) {
         let hex_array = [{key:0,val:"0000"},{key:1,val:"0001"},{key:2,val:"0010"},{key:3,val:"0011"},{key:4,val:"0100"},{key:5,val:"0101"},{key:6,val:"0110"},{key:7,val:"0111"},
             {key:8,val:"1000"},{key:9,val:"1001"},{key:'a',val:"1010"},{key:'b',val:"1011"},{key:'c',val:"1100"},{key:'d',val:"1101"},{key:'e',val:"1110"},{key:'f',val:"1111"}]

         let value=""
         for(let i=0;i<str.length;i++){
             for(let j=0;j<hex_array.length;j++){
                 if(str.charAt(i).toLowerCase()== hex_array[j].key){
                     value = value.concat(hex_array[j].val)
                     break
                 }
             }
         }
         console.log(value)
         return value
     }*/
</script>
</html>
